using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Home_ManagePlacements : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
            this.txtNewPtStartDate.Text = DateTime.Now.ToShortDateString();
            this.txtNewPTEndDate.Text = DateTime.Now.AddMonths(1).ToShortDateString();
            this.calNewPTStartDate.SelectedDate = DateTime.Now;
            this.calNewPTStartDate.VisibleDate = DateTime.Now;
            this.calNewPTEndDate.SelectedDate = DateTime.Now.AddMonths(1);
            this.calNewPTEndDate.VisibleDate = DateTime.Now.AddMonths(1);
       
    }
    protected void gvPlacementTypes_DataBound(object sender, EventArgs e)
    {

    }
    protected void gvPlacementTypes_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridViewRow gvr = e.Row;

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string toolTip = this.GetUnitName(DataBinder.Eval(e.Row.DataItem, "UnitId").ToString());
            toolTip += "<br /> Semester:" + DataBinder.Eval(e.Row.DataItem, "SemesterId").ToString();
            toolTip += "<br /> Year:" + DataBinder.Eval(e.Row.DataItem, "Year").ToString();
            toolTip += "<br /> Campus:" + this.GetCampusName(DataBinder.Eval(e.Row.DataItem, "PlacementTypeId").ToString());
            toolTip += "<br /> Category:" + this.GetCategoryName(DataBinder.Eval(e.Row.DataItem, "CategoryId").ToString());
            toolTip += "<br /> Start Date:" + (Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "DateStart"))).ToShortDateString();
            toolTip += "<br /> End Date:" + (Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "DateEnd"))).ToShortDateString();


            gvr.Cells[1].Attributes.Add("onmouseover", "showToolTip(event,'" + toolTip + "');");
            gvr.Cells[1].Attributes.Add("onmouseout", "hideToolTip();");
            gvr.Cells[2].Attributes.Add("onmouseover", "showToolTip(event,'" + toolTip + "');");
            gvr.Cells[2].Attributes.Add("onmouseout", "hideToolTip();");
            gvr.Cells[3].Attributes.Add("onmouseover", "showToolTip(event,'" + toolTip + "');");
            gvr.Cells[3].Attributes.Add("onmouseout", "hideToolTip();");

        }

    }
    protected void gvPlacementTypes_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected string GetUnitName(string unitId)
    {
        PlacementManager.UnitComponent oUnitC = new PlacementManager.UnitComponent();
        return oUnitC.SelectOne(Int32.Parse(unitId)).UnitCode + " - " + oUnitC.SelectOne(Int32.Parse(unitId)).UnitName;
    }
    protected string GetCampusName(string sPlacementTypeId)
    {
        PlacementManager.PlacementTypeComponent oPlacementTypeC = new PlacementManager.PlacementTypeComponent();
        int campusId = oPlacementTypeC.SelectOne(Int32.Parse(sPlacementTypeId)).CampusId;

        PlacementManager.CampusComponent oCampusC = new PlacementManager.CampusComponent();
        return oCampusC.SelectOne(campusId).CampusCode;

    }
    protected string GetCategoryName(string categoryId)
    {
        PlacementManager.CategoryComponent oCategoryC = new PlacementManager.CategoryComponent();
        return oCategoryC.SelectOne(Int32.Parse(categoryId)).CategoryName;
    }
    protected string GetStatus(string placementTypeId)
    {
        PlacementManager.PlacementTypeComponent oPTC = new PlacementManager.PlacementTypeComponent();
        PlacementManager.PlacementType oPT = oPTC.SelectOne(Int32.Parse(placementTypeId));

        string toReturn = string.Empty;

        if (oPT.DateStart > DateTime.Now)
        {
            toReturn = "The placement period has not commenced yet.";
        }

        if (oPT.DateStart < DateTime.Now && oPT.DateEnd > DateTime.Now)
        {
            toReturn = "The placement is in progress";
        }

        if (DateTime.Now > oPT.DateEnd)
        {
            toReturn = "The placement period has ended.";
        }
        return toReturn;
    }
    protected string GetAttendancePattern(string attendancePatternId)
    {
        PlacementManager.AttendancePatternComponent oAPC = new PlacementManager.AttendancePatternComponent();
        return oAPC.SelectOne(Int32.Parse(attendancePatternId)).AttendancePatternName;
    }
    protected void fvPlacementTypeDetails_DataBound(object sender, EventArgs e)
    {
        if (this.gvPlacementTypes.Rows.Count > 0)
        {
            //Check to see whether this placement can be edited or not
            PlacementManager.PlacementTypeComponent oPTC = new PlacementManager.PlacementTypeComponent();
            DateTime DateStart = oPTC.SelectOne(Int32.Parse(this.gvPlacementTypes.SelectedDataKey.Value.ToString())).DateStart;
            DateTime DateEnd = oPTC.SelectOne(Int32.Parse(this.gvPlacementTypes.SelectedDataKey.Value.ToString())).DateEnd;

            if (DateTime.Now > DateEnd)
            {
                //this.fvPlacementTypeDetails.ChangeMode(FormViewMode.ReadOnly);
            }
        }
    }
    protected void fvPlacementTypeDetails_ItemUpdating(object sender, FormViewUpdateEventArgs e)
    {
        PlacementManager.PlacementTypeComponent oPTC = new PlacementManager.PlacementTypeComponent();
        PlacementManager.PlacementType oPT = oPTC.SelectOne(Int32.Parse(this.gvPlacementTypes.SelectedDataKey.Value.ToString()));

        e.NewValues.Add("IsActive", oPT.IsActive);
        e.NewValues.Add("CreatedBy", oPT.CreatedBy);
        e.NewValues.Add("DateCreated", oPT.DateCreated);
        e.NewValues.Add("DateUpdated", DateTime.Now);
        e.NewValues.Add("PlacementTypeId", oPT.PlacementTypeId);
        e.NewValues.Add("UpdatedBy", User.Identity.Name);

        //Remove these to avoid problems inserting the text straight in to the db later on
        e.NewValues.Remove("DateEnd");
        e.NewValues.Remove("DateStart");

        //Pick up the dates and explicitly convert them
        e.NewValues.Add("DateEnd", Convert.ToDateTime(((TextBox)this.fvPlacementTypeDetails.FindControl("DateEndTextBox")).Text));
        e.NewValues.Add("DateStart", Convert.ToDateTime(((TextBox)this.fvPlacementTypeDetails.FindControl("DateStartTextBox")).Text));

    }
    protected void fvPlacementTypeDetails_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
    {
        this.gvPlacementTypes.DataBind();
    }
    protected void calStartDate_SelectionChanged(object sender, EventArgs e)
    {
        TextBox DateStartTextBox = (TextBox)this.fvPlacementTypeDetails.FindControl("DateStartTextBox");
        Calendar calStartDate = (Calendar)this.fvPlacementTypeDetails.FindControl("calStartDate");
        if (DateStartTextBox != null)
        {
            DateStartTextBox.Text = calStartDate.SelectedDate.ToShortDateString();
        }
    }
    protected void calEndDate_SelectionChanged(object sender, EventArgs e)
    {
        TextBox DateEndTextBox = (TextBox)this.fvPlacementTypeDetails.FindControl("DateEndTextBox");
        Calendar calEndDate = (Calendar)this.fvPlacementTypeDetails.FindControl("calEndDate");
        if (DateEndTextBox != null)
        {
            DateEndTextBox.Text = calEndDate.SelectedDate.ToShortDateString();
        }
    }
    protected void gvNotes_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        PlacementManager.NoteComponent oNoteC = new PlacementManager.NoteComponent();
        PlacementManager.Note oNote = oNoteC.SelectOne(Int32.Parse(this.gvNotes.DataKeys[this.gvNotes.EditIndex].Value.ToString()));

        e.NewValues.Add("CreatedBy", oNote.CreatedBy);
        e.NewValues.Add("DateCreated", oNote.DateCreated);
        e.NewValues.Add("DateUpdated", DateTime.Now);
        e.NewValues.Add("IsActive", oNote.IsActive);
        e.NewValues.Add("EntityId", oNote.EntityId);
        e.NewValues.Add("NoteId", oNote.NoteId);
        e.NewValues.Add("NoteTypeId", oNote.NoteTypeId);
        e.NewValues.Add("UpdatedBy", User.Identity.Name);
    }
    protected void gvNotes_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "DeleteNote")
        {
            PlacementManager.NoteComponent oNoteC = new PlacementManager.NoteComponent();
            PlacementManager.Note oNote = oNoteC.SelectOne(Int32.Parse(e.CommandArgument.ToString()));
            oNote.IsActive = false;
            oNoteC.Update(oNote);
            this.gvNotes.DataBind();

        }
    }
    protected void btnSaveNote_Click(object sender, EventArgs e)
    {
        if (this.gvPlacementTypes.Rows.Count > 0)
        {
            PlacementManager.NoteComponent oNoteC = new PlacementManager.NoteComponent();
            PlacementManager.Note oNote = new PlacementManager.Note();
            oNote.CreatedBy = User.Identity.Name;
            oNote.DateCreated = DateTime.Now;
            oNote.DateUpdated = DateTime.Now;
            oNote.EntityId = Int32.Parse(this.gvPlacementTypes.SelectedValue.ToString());
            oNote.IsActive = true;
            oNote.NoteTypeId = 3;
            oNote.UpdatedBy = User.Identity.Name;
            oNote.NoteText = this.txtAddNewNote.Text;
            oNoteC.Insert(oNote);

            this.gvNotes.DataBind();
            this.txtAddNewNote.Text = string.Empty;
        }
    }
    protected void ddUnitFilter_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.gvPlacementTypes.SelectedIndex = 0;
    }
    protected void ddCampusFilter_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.gvPlacementTypes.SelectedIndex = 0;
    }
    protected void ddSemesterFilter_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.gvPlacementTypes.SelectedIndex = 0;
    }
    protected void ddYearFilter_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.gvPlacementTypes.SelectedIndex = 0;
    }
    protected void gvPlacementTypes_PageIndexChanged(object sender, EventArgs e)
    {
        this.gvPlacementTypes.SelectedIndex = 0;
    }
    protected void cbInsertAccruesPoints_CheckedChanged(object sender, EventArgs e)
    {
        if (this.cbInsertAccruesPoints.Checked)
        {
            this.txtNewNumberOfPoints.Enabled = true;
            this.txtNewRadiusPoints.Enabled = true;
        }
        else
        {
            this.txtNewNumberOfPoints.Enabled = false;
            this.txtNewRadiusPoints.Enabled = false;
        }
    }
    protected void btnInsertNewPT_Click(object sender, EventArgs e)
    {

        //TODO: Error checking
        PlacementManager.PlacementTypeComponent oPTC = new PlacementManager.PlacementTypeComponent();
        PlacementManager.PlacementType oPT = new PlacementManager.PlacementType();
        oPT.AttendancePatternId = Int32.Parse(this.ddNewPTAttendancePattern.SelectedValue);
        oPT.CategoryId = Int32.Parse(this.ddNewPTCategory.SelectedValue);
        oPT.CampusId = Int32.Parse(this.ddNewPTCampus.SelectedValue);
        oPT.CreatedBy = User.Identity.Name;
        oPT.DateCreated = DateTime.Now;
        oPT.DateEnd = Convert.ToDateTime(this.txtNewPTEndDate.Text);
        oPT.DateStart = Convert.ToDateTime(this.txtNewPtStartDate.Text);
        oPT.IsActive = true;
        oPT.DateUpdated = DateTime.Now;
        oPT.PlacementTypeName = this.txtNewPTName.Text;
        oPT.RequiredHours = Int32.Parse(this.txtNewPTRequiredHours.Text);
        oPT.SemesterId = Int32.Parse(this.ddNewPTSemester.SelectedValue);
        oPT.UnitId = Int32.Parse(this.ddNewPTUnit.SelectedValue);
        oPT.UpdatedBy = User.Identity.Name;
        oPT.Year = Int32.Parse(this.ddNewPTYear.SelectedValue);

        oPTC.Insert(oPT);
        this.gvPlacementTypes.DataBind();
        this.txtNewPtStartDate.Text = string.Empty;
        this.txtNewPTEndDate.Text = string.Empty;
        this.txtNewPTName.Text = string.Empty;
        this.txtNewPTRequiredHours.Text = "0";

        //Rebind the placementypes list
        this.gvPlacementTypes.DataBind();

        this.fvPlacementTypeDetails.DataBind();

    }
    protected void calNewPTStartDate_SelectionChanged(object sender, EventArgs e)
    {
        this.txtNewPtStartDate.Text = this.calNewPTStartDate.SelectedDate.ToShortDateString();
    }
    protected void calNewPTEndDate_SelectionChanged(object sender, EventArgs e)
    {
        this.txtNewPTEndDate.Text = this.calNewPTEndDate.SelectedDate.ToShortDateString();
    }
}

